/*------------------------------------------------------------------------------
* Project - Strength in Numbers? Gender Composition, Leadership, and Women’s Influence in Teams?
* Researchers:  Christopher F. Karpowitz, Stephen D. O’Connell, Jessica Preece, and Olga Stoddard
* Purpose: Main do-file. Runs a push-button replication of "Strength in Numbers? Gender Composition, Leadership, and Women’s Influence in Teams?"
* Created on: 10/10/2023
* Last edited: 10/26/2023
* Edits history:
	* Setting a correct directory for Windows machines
--------------------------------------------------------------------------------*/

/* Instructions: 
	1. Set the path to this replication package in the areas marked "SET DIRECTORY HERE" (Lines 31 and 62)
	2. Set the global "sim": it takes 1 if you would like to run the simulations necessary for figure 6 and a few of the appendix tables/figures and 0 if not.
	3. If you are in a Windows machine, set the correct path according to the version of your R in the area marked "CHANGE VERSION NUMBER, IF WINDOWS". 
	   We recommend greater or equal than 4.2.3 (Line 42)
	4. Run this do-file 
*/

/* Note:
- This main do-file uses the package rsource in order to run R scripts from the do-file. The syntax for doing this in macs and windows is slightly different. 
If you would like to run this manually all the way through on a windows operating system, please see the syntax in the "Archive" at the bottom of the do-file.
- This code detects what OS you are using and run R scripts accordingly.
- This code detects the R libraries that are being used and that you do not currently have installed, and proceeds to install them. See Step 4.
- Stata 18 version
- R >= 4.2.3
*/
***************************** Set up *******************************************
*** Step 1: basic global settings
clear all
set more off


global dir "~/Dropbox/StrengthInNumbersReplicationPackage/replicable_v0_10182023" //USER: SET DIRECTORY HERE
cd "$dir"

global sim = 0 // USER: Set 1 to run simulation

* Set the R path and options based on the operating system
if "`c(os)'" == "MacOSX" {
	display "MacOS system"
    global rpath `"/usr/local/bin/R"'
    global roptions `"--vanilla"'
}
else if "`c(os)'" == "Windows" {
	display "Windows system"
    global rpath `"C:\Program Files\R\R-4.3.1\bin\x64\Rterm.exe"' // USER: CHANGE VERSION NUMBER, IF WINDOWS
    global roptions `"--vanilla"'
}
else {
    di "Unsupported operating system"
    exit
}

* Step 2: Installing ssc commands necessary to run some chunks
***** The user-installed packages are listed below -- the code checks whether they are installed and if not, installs them. pdslasso and rlasso are also parts of the larger "lassopack" ssc package, so if you are having trouble with them, try running "ssc install lassopack".
global packages rsource estout outreg2 distinct balancetable mhtexp filelist
foreach package of global packages  {
		capture which `package'
		if _rc==111 ssc install `package'
}

* Step 3: Setting up R to run
rsource, terminator(END_OF_R) rpath($rpath) roptions($roptions)  maxlines(2)
	setwd("~/Dropbox/StrengthInNumbersReplicationPackage/replicable_v0_10182023"); // USER: SET DIRECTORY HERE
	q();
	END_OF_R

* Step 4: Installing libraries necessary to run R scripts

rsource using install_R_packages.R, rpath($rpath) roptions($roptions)

**************************** Main tables: **************************************
*** Only one table created by code in the main paper
/* The basic structure of the code below will be used across all of the replication. It:
	- Changes the working directory to the folder containing the replication materials for that table/figure
	- Checks if there are any do files in the folder, and if so runs them all
	- Checks if there are any R scripts in the folder, and if so runs them all
	- Changes the directory back to the main directory
*/
	cd "table_2"
	// Checking for do files
	filelist, pattern("*.do")
	count
	// Running them if they exist
	if `r(N)' >0{
		loc rsc = ""
		forvalues x = 1/`r(N)'{
			loc iter = filename[`x']
			loc rsc "`rsc' `iter'"
			}
		foreach file in `rsc' {
			   do `file'
			}

		}
	// Checking for R scripts
	filelist , pattern("*.R")
	count
	
	// Running them if they exist
	if `r(N)'>0{

		loc rsc = ""
		forvalues y = 1/`r(N)'{
			loc rsc "`rsc' filename[`y']"
		}
		foreach file of local rsc{
			    rsource using `file', rpath($rpath) roptions($roptions) 
		}
	} 

		
	cd "$dir"
************************* Main figures *****************************************
// The same as above, but looping through each of the first 5 main figure folders (excluding six for now b/c it needs simulated data)
forvalues x=1/5{
	****** Main figures:
	cd "figure_`x'"
	
		filelist, pattern("*.do")
	count
	if `r(N)' >0{
		loc rsc = ""
		forvalues x = 1/`r(N)'{
			loc iter = filename[`x']
			loc rsc "`rsc' `iter'"
		}
		foreach file in `rsc' {
			   do `file'
			}

		}

	
	filelist , pattern("*.r")
	count
	
	if `r(N)'>0{
		loc rsc = ""
		forvalues y = 1/`r(N)'{
			loc iter = filename[`y']
			loc rsc "`rsc' `iter'"
		}
		foreach file of local rsc{
			    rsource using `file', rpath($rpath) roptions($roptions) 
		}
	} 
		cd "$dir"
	}

**************************** Run simulations: **********************************
// Only running if the global sim is marked as 1
if $sim == 1{
cd "simulations"
rsource using simulation_1.R, rpath($rpath) roptions($roptions) 

rsource using preparing_simulation_2.R, rpath("/usr/local/bin/R") roptions(`"--vanilla"') 

rsource using simulation_2.R, rpath($rpath) roptions($roptions)
cd "$dir"
}

**************** Figure 6: *****************************************************
// Run this figure if and only if sim was marked as 1 and you have the inputs necessary from simulation
cd "figure_6"
do "figure_6.do"
cd "$dir"
rsource using figure_6/figure_6.r, rpath($rpath) roptions($roptions)


********************** Appendix tables *****************************************
// Code doesn't create tables 2 or 5, so creating a local with all numbers 1-19 other than those
global app_tabs "1 3 4"
forvalues x = 6/19 {
    global app_tabs "$app_tabs `x'"
}

foreach x in $app_tabs{
	
	display "table_a`x'"
	
}
// Looping through all of those numbers and running the table code
foreach x in $app_tabs{
	****** Main tables:
	cd "table_a`x'"
	
	filelist, pattern("*.do")
	count
	if `r(N)' >0{
		loc rsc = ""
		forvalues x = 1/`r(N)'{
			loc iter = filename[`x']
			loc rsc "`rsc' `iter'"
		}
		foreach file in `rsc' {
			   do `file'
		}

	}

	
	filelist , pattern("*.r")
	count
	if `r(N)'>0{
		loc rsc = ""
		forvalues y = 1/`r(N)'{
			loc iter = filename[`y']
			loc rsc "`rsc' `iter'"
		}
		foreach file in `rsc' {
			    rsource using `file', rpath($rpath) roptions($roptions) 
		}
	} 
	
	
	filelist , pattern("*.R")
	count
	if `r(N)'>0{
		loc rsc = ""
		forvalues y = 1/`r(N)'{
			loc iter = filename[`y']
			loc rsc "`rsc' `iter'"
		}
		foreach file in `rsc' {
			    rsource using `file', rpath($rpath) roptions($roptions)
		}
	} 
	
	cd "$dir"
	}
	
	
*************************** Appendix figures ***********************************
// Looping through the 5 appendix figures. Figure a6 requires simulation
forvalues x =1/5{
	cd "figure_a`x'"
	
	filelist, pattern("*.do")
	count
	if `r(N)' >0{
		loc rsc = ""
		forvalues x = 1/`r(N)'{
			loc iter = filename[`x']
			loc rsc "`rsc' `iter'"
		}
		foreach file in `rsc' {
			   do `file'
			}

		}

	
	filelist , pattern("*.r")
	count
	
	if `r(N)'>0{
		loc rsc = ""
		forvalues y = 1/`r(N)'{
			loc iter = filename[`y']
			loc rsc "`rsc' `iter'"
		}
		foreach file of local rsc{
			    rsource using `file', rpath($rpath) roptions($roptions)
		}
	}
	
	
	filelist , pattern("*.R")
	count
	
	if `r(N)'>0{
		loc rsc = ""
		forvalues y = 1/`r(N)'{
			loc iter = filename[`y']
			loc rsc "`rsc' `iter'"
		}
		foreach file of local rsc{
			    rsource using `file', rpath($rpath) roptions($roptions)
		}
	} 
		cd "$dir"
	}
	
************** Appendix tables/figs that need simulation data ******************
// Please execute scripts in the simulations folder before replicating this table.
// Table A15 requires input from figure_5 folder.
cd "$dir"
rsource using table_a15/table_a15.R, rpath($rpath) roptions($roptions)

cd "$dir"
rsource using figure_a6/figure_a6.R, rpath($rpath) roptions($roptions)

****************************** ARCHIVE *****************************************
//// ARCHIVE: Rsource if using windows
/*
    rsource using Scripts/Main/Figure_1.R, rpath(`"c:\r\R-4.2.3\bin\Rterm.exe"') roptions(`"--vanilla"')  // change version number, if necessary
*/
